<?php
    class Main_Controller_Plugin_Language extends Zend_Controller_Plugin_Abstract
    {
        public function routeShutdown(Zend_Controller_Request_Abstract $request)
        {
            $locale = new Zend_Locale();
            $options = array('scan'=>Zend_Translate::LOCALE_FILENAME);
            $translate = new Zend_Translate('Array',BASE_PATH.'/application/languages/','auto',$options);
            $requestParams = $this->getRequest()->getParams();
            $language = (isset($requestParams['lang'])) ? $requestParams['lang'] : false;
            if($language == false) {
                $language = ($translate->isAvailable($locale->getLanguage())) ? $locale->getLanguage() : 'vi';
            }
            if(!$translate->isAvailable($language)) {
                throw new Zend_Controller_Action_Exception('Đường dẫn không tồn tại',404);
            }
            else {
                $locale->setLocale($language);
                $translate->setLocale($locale);
                setcookie('lang',$locale->getLanguage(),null);
                Zend_Registry::set('Zend_Locale', $locale);
                Zend_Registry::set('Zend_Translate', $translate);
            }
        }
    }
